

/********************************************************************/
/*                                                                  */ 
/* Export variables used for regressions to STATA                   */
/*                                                                  */ 
/********************************************************************/




libname common 'G:\Data\Workdata\707677\Common\01Data\01SAS';




/*****************************/
/* Load cross-sectional data */
/*****************************/


/* IVs */


data iv;

set common.ivs;

keep pnr court obs_lawyer lawyerid;

run;




/* Base data */


data list;

set common.list;

keep pnr schooling;

run;




/* More base data */



data mlist;

set common.panel;

keep pnr granted pyear male age0;

run;



proc sort data=mlist nodupkey;

by pnr;

run;





/* Merge cross-sectionial data */



proc sql;

create table list as select *

from list as a left join

mlist as b on 

a.pnr=b.pnr;

quit;




proc sql;

create table list as select *

from list as a left join

iv as b on 

a.pnr=b.pnr;

quit;




proc datasets library=work nolist;

delete iv mlist;

quit;





/*****************************************/
/* Add data on outcomes before treatment */
/*****************************************/



data pre;

set common.panel;

keep pnr time pstill

AKTIV_real

PASSIV_real

FORMUE_real

erhvervsindk_real

employed_pstill

unemployed_pstill

out_of_lf_pstill

kontant_real

social

legmarried

house

ie_type

antpersf

edu_type

edu_type_extended

DISPON_NY_real

ANTBOERNF

TIMELON_real

antpersf_d;
 
if time<0;

run;




data before;

set pre;

keep pnr;

if time=-4;

run;





%macro prelevel(number);



data before&number.;

set pre;

if time=-&number.;

run;



data before&number.;

set before&number.;

rename AKTIV_real=AKTIV_M&number.;

rename PASSIV_real=PASSIV_M&number.;

rename FORMUE_real=FORMUE_M&number.;

rename erhvervsindk_real=erhvervsindk_M&number.;

rename employed_pstill=employed_pstill_M&number.;

rename unemployed_pstill=unemployed_pstill_M&number.;

rename out_of_lf_pstill=out_of_lf_pstill_M&number.;

rename kontant_real=kontant_M&number.;

rename social=social_M&number.;

rename legmarried=legmarried_M&number.;

rename house=house_M&number.;

rename TIMELON_real=TIMELON_real_M&number.;

rename ie_type=ie_type_M&number.;

rename antpersf=antpersf_M&number.;

rename DISPON_NY_real=DISPON_NY_real_M&number.;

rename edu_type=edu_type_M&number.;

rename edu_type_extended=edu_type_extended_M&number.;

rename ANTBOERNF=ANTBOERNF_M&number.;

rename antpersf_d=antpersf_d_M&number.;

drop time;

run;




proc sql;

create table before as select *

from before as a left join

before&number. as b on

a.pnr=b.pnr;

quit;



proc datasets library=work nolist;

delete before&number.;

quit;




%mend;


%prelevel(12);
%prelevel(11);
%prelevel(10);

%prelevel(9);

%prelevel(8);

%prelevel(7);

%prelevel(6);

%prelevel(5);

%prelevel(4);

%prelevel(3);

%prelevel(2);

%prelevel(1);




proc sql;

create table list as select *

from list as a left join

before as b on

a.pnr=b.pnr;

quit;




/************************************/
/* Retrieve panel data on outcomes  */
/************************************/ 



data panel;

set common.panel;

run;




data panel;

set panel;

keep pnr age time aar kom

TIMELON_real

AKTIV_real

PASSIV_real

FORMUE_real

KOEJD_real

BANKGAELD_real

mortgage_real

erhvervsindk_real

house

employed_pstill

unemployed_pstill

out_of_lf_pstill

tilbtot_real

disability

kontant_real

social

DISPON_NY_real

netovskud_real

legmarried;   


run;




data panel;

set panel;

age2=age*age;

run;




proc sql;

create table panel as select *

from panel as a left join

list as b on

a.pnr=b.pnr;

quit;



proc sort data=panel;

by pnr time;

run;








/************************/
/* Export data to STATA */
/************************/



proc sort data=panel nodupkey;

by pnr aar;

run;





proc export data=panel outfile= "G:\Data\Workdata\707677\FKYY7677\Debt_relief\Statafiles\Revision\Data\panel_iv_long.dta" replace; 

run;




proc datasets library=work kill nolist;

quit;


